package com.wx.registerclient.listener;


import com.wx.registerclient.configuration.NBServerConfigProperties;
import org.apache.http.HttpEntity;

import org.apache.http.client.config.RequestConfig;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import org.apache.http.util.EntityUtils;
import org.nd4j.shade.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

import org.springframework.stereotype.Component;

import java.io.IOException;



/**
 * 在项目启动以后执行
 */
@Component
public class RegistListener implements ApplicationRunner {

    @Autowired
    private NBServerConfigProperties properties;

    @Override
    public void run(ApplicationArguments args) {

        // 使用HttpClient发送注册信息
        System.out.println("=====================");
        System.out.println("开始注册信息");
        System.out.println("=====================");
        // 获取注册中心地址信息
        String registerHost = properties.getRegisterHost();
        String registerPort = properties.getRegisterPort();
        // 拼接注册中心地址
        String url = "http://" + registerHost + ":" + registerPort + "/register/story";

        // 封装注册参数  pairs.add(new BasicNameValuePair());

        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        CloseableHttpResponse response = null;
        try {
            HttpPost httpPost = new HttpPost(url);
            RequestConfig requestConfig = RequestConfig.custom()
                    .setSocketTimeout(2000) //服务器响应超时时间
                    .setConnectTimeout(2000) //连接服务器超时时间
                    .build();
            httpPost.setConfig(requestConfig);
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(properties);

            StringEntity entity = new StringEntity(json, "utf-8");//也可以直接使用JSONObject
            httpPost.setEntity(entity);
            httpPost.setHeader("Content-Type", "application/json;charset=utf8");
            // 由客户端执行(发送)请求
            response = httpClient.execute(httpPost);
            System.out.println("响应状态为:" + response.getStatusLine());
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
